package online.flowerinsnow.minecraft.bukkit.manhunt.session.item;

import org.bukkit.inventory.ItemStack;
import online.flowerinsnow.minecraft.bukkit.manhunt.util.VersionUtils;

import java.lang.reflect.InvocationTargetException;
import java.util.function.Consumer;

public class ItemStackSession {
    private ItemStack item;
    private Object nmsItem;

    public ItemStackSession(ItemStack item) {
        setItem(item);
    }

    public ItemStack getItem() {
        return item;
    }

    public Object getNmsItem() {
        return nmsItem;
    }

    public ItemStackSession setItem(ItemStack item) {
        this.item = item;
        this.nmsItem = VersionUtils.getNMSItemStack(item);
        return this;
    }

    public ItemStackSession setNmsItem(Object nmsItem) {
        this.nmsItem = nmsItem;
        this.item = VersionUtils.getBukkitItemStack(nmsItem);
        return this;
    }

    @SuppressWarnings("ConstantConditions")
    public Object getNBTTagCompound() {
        try {
            return VersionUtils.NMSMethods.ITEM_STACK__GET_OR_CREATE_TAG.invoke(nmsItem);
        } catch (IllegalAccessException | InvocationTargetException e) {
            throw new IllegalStateException(e);
        }
    }

    @SuppressWarnings("ConstantConditions")
    public String getNBTTagCompoundString(String key) {
        try {
            return (String) VersionUtils.NMSMethods.NBT_TAG_COMPOUND__GET_STRING.invoke(getNBTTagCompound(), key);
        } catch (IllegalAccessException | InvocationTargetException e) {
            throw new IllegalStateException(e);
        }
    }

    @SuppressWarnings("ConstantConditions")
    public boolean getNBTTagCompoundBoolean(String key) {
        try {
            return (boolean) VersionUtils.NMSMethods.NBT_TAG_COMPOUND__GET_BOOLEAN.invoke(getNBTTagCompound(), key);
        } catch (IllegalAccessException | InvocationTargetException e) {
            throw new IllegalStateException(e);
        }
    }

    @SuppressWarnings("ConstantConditions")
    public ItemStackSession setNBTTagCompound(Object tag) {
        try {
            VersionUtils.NMSMethods.ITEM_STACK__SET_TAG.invoke(this.nmsItem, tag);
        } catch (IllegalAccessException | InvocationTargetException e) {
            throw new IllegalStateException(e);
        }
        return setNmsItem(this.nmsItem);
    }

    @SuppressWarnings("ConstantConditions")
    public ItemStackSession setNBTTagCompoundString(String key, String value) {
        return autoNBTTagAction(tag -> {
            try {
                VersionUtils.NMSMethods.NBT_TAG_COMPOUND__SET_STRING.invoke(tag, key, value);
            } catch (IllegalAccessException | InvocationTargetException e) {
                throw new IllegalStateException(e);
            }
        });
    }

    @SuppressWarnings("ConstantConditions")
    public ItemStackSession setNBTTagCompoundString(String key, boolean value) {
        return autoNBTTagAction(tag -> {
            try {
                VersionUtils.NMSMethods.NBT_TAG_COMPOUND__SET_BOOLEAN.invoke(tag, key, value);
            } catch (IllegalAccessException | InvocationTargetException e) {
                throw new IllegalStateException(e);
            }
        });
    }

    public ItemStackSession autoNBTTagAction(Consumer<Object> action) {
        Object tag = getNBTTagCompound();
        action.accept(tag);
        return setNBTTagCompound(tag);
    }
}
